/**
 * 自定义预加载图片的类
 * param imgList传入的待加载图片数组
 * param imgLoadAll待加载总数量
 * param count已加载数量
 */
//判断是否微信登陆
var ua = window.navigator.userAgent.toLowerCase();
function isWeiXin() {
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
}

class PreloadImg {
    constructor(imgList) {
        this.imgList = imgList;
        this.imgLoadAll = this.imgList.length;
        this.count = 0;
        this.per = 0;
    }
    // 遍历待加载的图片
    loadImg() {
        var that = this;
        that.imgList.forEach(function (src, i) {
            var newsrc = src.substring(src.length - 3);
            if (newsrc === 'mp3') {
                var audio = new Audio(src);
                var loadType = false;
                if (isWeiXin()) {
                    document.addEventListener("WeixinJSBridgeReady", function () {
                        audio.load();
                        audio.addEventListener('canplaythrough', function () {
                            if (!loadType) {
                                loadType = true;
                                that.callback();
                            }
                        }, false);
                    }, false);
                } else {
                    audio.load();
                    audio.addEventListener('canplaythrough', function () {
                        if (!loadType) {
                            loadType = true;
                            console.log('歌曲下载完毕');
                            that.callback();
                        }
                    }, false);
                }
            } else {
                var img = new Image();
                img.onload = function () {
                    that.callback();
                }
                img.src = src;
            }
        });
    }
    // 加载完毕回调
    callback() {
        var that = this;
        that.count = that.count + 1;
        console.log(that.count)
        that.per = Math.floor((that.count / that.imgLoadAll) * 100);
        that.changePer(that.per);
        if (that.per == 100) {
            console.log('加载完毕');
        }
    }
    // 更改DOM百分比样式
    changePer(per) {
        $(".perc_txt").html(per + "%");
    }
}

